/*
 * @Author: 15097322811 1447934599@qq.com
 * @Date: 2024-01-16 10:17:31
 * @LastEditors: 15097322811 1447934599@qq.com
 * @LastEditTime: 2024-02-04 18:17:32
 * @Description:
 */
import http from "utils/http.js";
const bgm = uni.createInnerAudioContext();
// 获取视频
function getMusic() {
  http({
    url: "api/common/get_general",
  })
    .then((res) => {
      if (res.data.code == 1) {
        if (res.data.data.bg_music_url) {
          bgm.src = res.data.data.bg_music_url;
        } else {
          bgm.src =
            "https://v3mh6.oss-cn-beijing.aliyuncs.com/kaijiangdonghua/music.mp3";
        }
      }
    })
    .catch((err) => {});
}
getMusic();
bgm.loop = true; //循环播放
bgm.autoplay = true; //自动播放
bgm.sessionCategory = "soloAmbient"; //在后台时不播放，如有其他播放，会暂停其他播放（但在移动端h5中 后台不播放失效）（其他属性自行百度）
bgm.volume = 0.4; //音量
var music = {
  //mute 表示是否是静音，，默认静音
  playBgm({ mute = false }) {
    if (!bgm) return;
    if (mute) {
      bgm.stop();
      bgm.loop = false;
      bgm.autoplay = false;
    } else {
      bgm.play();
      bgm.loop = true;
      bgm.autoplay = true;
    }

    bgm.onStop(() => {});
    bgm.onPlay(() => {});
    bgm.onError((res) => {});
  },
  destroyBgm() {
    bgm.destroy();
  },
};
module.exports = music;
